package com.gxa.pingan.controller;


import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.pojo.CarBrand;
import com.gxa.pingan.service.CarBrandService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping(value = "/carBrand")
@Api(value = "品牌控制器",tags = "品牌的接口")
public class CarBrandController {

    private  final  static  Integer SIZE=10;

    @Autowired
   CarBrandService carBrandService;

    @PostMapping("/add")
    @ApiOperation(value = "添加品牌")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "carBrand" ,value = "传入的carbrand对象,不传ID值",type = "JSON")})
    public Map addCarBrand(@RequestBody CarBrand carBrand) throws Exception {
        carBrandService.add(carBrand);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }
    @PostMapping("/findAllByPage")
    @ApiOperation(value = "分页查询有哪些车型")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "page",value = "请输入页码"),@ApiImplicitParam(name = "size",value = "每页的记录数数")})
    public Map findAllByPage(Integer page,Integer size){
        List<CarBrand> all = carBrandService.findAll();
        List<CarBrand> all1 = carBrandService.findAll(page, size);
        JsonResult jsonResult=new JsonResult();
        jsonResult.put("count",all.size());
        jsonResult.setCode("1");
        jsonResult.setData(all1);
        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("修改品牌的操作")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "carBrand",value = "请传入修改过后的carBrand类型的JSON字符串",type = "JSON")})
    public Map update(@RequestBody CarBrand carBrand) throws Exception {
            carBrandService.update(carBrand.getId(),carBrand);
            JsonResult jsonResult=new JsonResult();
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
            return jsonResult.getDataMap();
    }

    @PostMapping("/findAll")
    @ApiOperation("这是品牌全查询的方法")
    public Map findAll(){
        List<CarBrand> all = carBrandService.findAll();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(all);
        return jsonResult.getDataMap();
    }
}
